<?php
# 文件名称:fdparameter.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
class fdparameter extends C_controller {
	function fdparameter()
	{
		parent::C_controller();
		//设置语言
		$this->setLanguageFile( 'fdparameter' );
		$this->registerTask( 'apply', 	'save'  );
	}
	
	function display()
	{
		$SYS = &get_instance();
		//判断是否有权限
		$this->permit('fdparameter.display');
		//应用还是保存任务
		if($this->getTask()=='apply')
		{
			$tmp[] = $SYS->request->getVar('id', 0, 'post', 'int');
			$SYS->request->setVar('cid', $tmp, 'post', 'int');
			parent::display($SYS->request->getVar('apply',''));
		}	
		else
		{
			parent::display($this->getTask());
		}
	}
	function saveall()
	{
		C_request::checkToken();
		//判断是否有权限
		$this->permit('fdparameter.saveall');
		$SYS = &get_instance();
		//初始化数据
		$data = $SYS->request->get('post');
		$isNew	= true;
		$table = C_loader::getDTO('fdparameter');
		foreach ($data['cid'] as $id)
		{
			// 绑定数据
			if (!$table->bindall($data,$id)) {
				$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
				$this->display();
				return;
			}
			// 验证数据
			if (!$table->check()) {
				$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
				$this->display();
				return;
			}
			
			// 保存
			if (!$table->store()) {
				$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
				$this->display();
				return;
			}
			//写入多语言字段
			$fieldstable = C_loader::getDTO('fields');
			$defaultlang = $SYS->lang->getDefault(1,'backend');
			
			$tmpfields = array();
			$tmpid = $defaultlang['id'];
			$tmpfields['lang'] = $tmpid;
			
			$fieldstable->load($data['nameid'][$id]);
			$tmpfields['fieldvalue'] = $data['name'][$id];
	
			// 绑定数据
			if (!$fieldstable->bind($tmpfields)) {
				$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
				$this->display();
				return;
			}
			// 保存
			if (!$fieldstable->store()) {
				$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
				$this->display();
				return;
			}
			//防止错误更新
			$fieldstable->id=0;
			unset($tmpfields);
			
		}
		$SYS->loader->setMessage(C_text::sprintf( 'Tip2'));
		$this->display();
	}
	function save()
	{
		C_request::checkToken();
		$this->permit('fdparameter.add');
		$SYS = &get_instance();
		// 初始化数据
		$data = $SYS->request->get('post');
		$data['id'] = $SYS->request->getVar('id', 0, 'post', 'int');
		$isNew	= true;
		$id = $data['id'];
		$table = C_loader::getDTO('fdparameter');
		if ($id > 0) {
			$table->load($id);
			$isNew = false;
		}
		if(!isset($data['use_ok']))
			$data['use_ok']=0;
		if(!isset($data['wr_ok']))
			$data['wr_ok']=0;
		// 绑定数据
		if (!$table->bind($data)) {
			$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
			$this->display();
			return;
		}
		// 验证数据
		if (!$table->check()) {
			$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
			$this->display();
			return;
		}
		// 保存
		if (!$table->store()) {
			$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
			$this->display();
			return;
		}
		//写入多语言字段
		$fieldstable = C_loader::getDTO('fields');
		$tmplang = $SYS->lang->getALL(1,'backend');
		for($i=0;$i<count($tmplang);$i++)
		{
			$tmpfields = array();
			$tmpid = $tmplang[$i]->id;
			$tmpfields['lang'] = $tmpid;
			if($id > 0)
			{
				$fieldstable->load($data['nameid'][$tmpid]);
				$tmpfields['fieldvalue'] = $data['name'][$tmpid];
			}else
			{
				$tmpfields['recordid'] = $table->get('id');
				$tmpfields['module'] = 3;
				$tmpfields['fieldname'] = "name";
				$tmpfields['fieldvalue'] = $data['name'][$tmpid];
			}			
			// 保存
			if (!$fieldstable->bind($tmpfields)) {
				$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
				$this->display();
				return;
			}
			// 保存
			if (!$fieldstable->store()) {
				$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
				$this->display();
				return;
			}
			//防止错误更新
			$fieldstable->id=0;
			unset($tmpfields);
		}
		
		$msg = '';
		// 更新自己时
		switch ( $this->getTask() )
		{
			case 'apply':
				$msg = C_text::sprintf( 'Tip1');
				break;
			case 'save':
			default:
				$msg = C_text::sprintf( 'Tip2');
				break;
		}
		//设置操作结果提示
		$SYS->loader->setMessage($msg);
		$this->display();
	}
	function remove()
	{
		C_request::checkToken();
		$this->permit('fdparameter.remove');
		$SYS = &get_instance();
		$cid = $SYS->request->getVar( 'cid', array(), '', 'array' );
		$table = C_loader::getDTO('fdparameter');
		if (count( $cid ) < 1) {
			return ;
		}
		$msg = '';
		foreach ($cid as $id)
		{
			$table->load($id);
			//删除参数多语言字段
			$query = 'DELETE '.
					' FROM '.$table->_db->_table_prefix.'fields'.
					' WHERE recordid='.$id.
					' AND module=3'
			;
			$table->_db->setQuery($query);
			$table->_db->query();
			//删除列表多语言字段
			//mysql4不支持子查询
			$query = 'SELECT id '.
					' FROM '.$table->_db->_table_prefix.'fdlist'.
					' WHERE bigid='.$id
			;
			$table->_db->setQuery($query);
			$tmpid=$table->_db->loadResultArray();
			$tmpid=implode(',',$tmpid);
			$query = 'DELETE '.
					' FROM '.$table->_db->_table_prefix.'fields'.					
					' WHERE recordid IN ('.$tmpid.
					' ) AND module=4'
			;
			$table->_db->setQuery($query);
			$table->_db->query();
			//删除列表
			$query = 'DELETE '.
					' FROM '.$table->_db->_table_prefix.'fdlist'.
					' WHERE bigid='.$id
			;
			$table->_db->setQuery($query);
			$table->_db->query();
			// 删除
			$table->delete();
			$msg = '';
		}
		$SYS->loader->setMessage(C_text::_('Tip3'));
		$this->display();
	}
	function cancel( )
	{
		$this->display();
	}
	function setlist()
	{
		C_request::checkToken();
		//切换到设置列表操作
		$this->permit('fdparameter.setlist');
		$SYS =& get_instance();
		$SYS->loader->helper('url');
		$cid = $SYS->request->getVar( 'cid', array(0), '', 'array' );
		redirect('fdlist/display/bigid/'.$cid[0]);
	}
	//复制
    function copyitems()
	{
		C_request::checkToken();
		$this->permit('fdparameter.copyitems');
		$SYS =& get_instance();
		//初始化数据
		$cid = $SYS->request->getVar( 'cid', array(0), '', 'array' );		
		$form = $SYS->request->getVar( 'form', 0, '', 'int' );
		if (count($cid) < 1 || $form == 0) 
		{
			$SYS->loader->setMessage(C_text::_('f_Tip3'),'error');
			$this->display();
			return;
		}
		//数据库对象
		$db = C_loader::getDBO();
		$total = count($cid);
		for ($i = 0; $i < $total; $i ++)
		{
			$row = C_loader::getDTO('fdparameter');
			// 主要sql语句
			$query = 'SELECT a.*' .
					' FROM '.$db->_table_prefix.'fdparameter AS a' .
					' WHERE a.id = '.(int) $cid[$i];
			$db->setQuery($query, 0, 1);
			$item = $db->loadObject();
			// 复制对应的值
			$row->id						= NULL;
			$row->no_order					= $item->no_order;
			$row->use_ok					= $item->use_ok;
			$row->wr_ok						= $item->wr_ok;
			$row->type						= $item->type;
			$row->form						= $form;
			$row->access					= $item->access;
			$row->params					= $item->params;
			
			if (!$row->check()) {
				$SYS->loader->setMessage(C_text::sprintf( 'f_Tip3'),'error');
				$this->display();
				return false;
			}
			
			if (!$row->store()) {
				$SYS->loader->setMessage(C_text::sprintf( 'f_Tip3'),'error');
				$this->display();
				return false;
			}
			//复制所属语言字段
			$parameterfieldstable = C_loader::getDTO('fields');
			if (!$parameterfieldstable->copy($item->id,$row->id,3)) {
				$SYS->loader->setMessage(C_text::sprintf( 'f_Tip3'),'error');
				$this->display();
				return false;
			}
			
			// 复制列表
			$query = 'SELECT a.*' .
					' FROM '.$db->_table_prefix.'fdlist AS a' .
					' WHERE a.bigid = '.(int) $item->id;
			$db->setQuery($query);
			$lists = $db->loadObjectList();
			for ($j = 0; $j < count($lists); $j ++)
			{
				$listtable = C_loader::getDTO('fdlist');
				$listtable->id					= NULL;
				$listtable->bigid				= $row->id;
				$listtable->no_order			= $lists[$j]->no_order;
				
				if (!$listtable->check()) {
					$SYS->loader->setMessage(C_text::sprintf( 'f_Tip3'),'error');
					$this->display();
					return false;
				}
				if (!$listtable->store()) {
					$SYS->loader->setMessage(C_text::sprintf( 'f_Tip3'),'error');
					$this->display();
					return false;
				}
				//复制所属语言字段
				$listfieldstable = C_loader::getDTO('fields');
				if (!$listfieldstable->copy($lists[$j]->id,$listtable->id,4)) {
					$SYS->loader->setMessage(C_text::sprintf( 'f_Tip3'),'error');
					$this->display();
					return false;
				}
			}			
		}
		$SYS->loader->setMessage(C_text::sprintf( 'f_Tip4'));
		$this->display();
	}
	//移动
	function moveitems()
	{
		C_request::checkToken();
		$this->permit('fdparameter.moveitems');
		$SYS =& get_instance();
		$cid = $SYS->request->getVar( 'cid', array(0), '', 'array' );		
		$form = $SYS->request->getVar( 'form', 0, '', 'int' );
		if (count($cid) < 1 || $form == 0) 
		{
			$SYS->loader->setMessage(C_text::_('f_Tip5'));
			$this->display();
			return;
		}
		$db = C_loader::getDBO();
		$total = count($cid);
		for ($i = 0; $i < $total; $i ++)
		{
			$row = C_loader::getDTO('fdparameter');
			//更新其所属表单
			$query = 'UPDATE '.$db->_table_prefix.'fdparameter ' .
					 ' SET form='.$form.
					' WHERE id = '.(int) $cid[$i];
			$db->setQuery($query);
			if (!$db->query()) {
				$SYS->loader->setMessage(C_text::sprintf( 'f_Tip5'),'error');
				$this->display();
				return false;
			}
		}
		$SYS->loader->setMessage(C_text::sprintf( 'f_Tip6'));
		$this->display();
	}
	
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>